package com.yuexun.beilunpatient.ui.registration.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuexun.beilunpatient.R;
import com.yuexun.beilunpatient.base.BaseEntity;
import com.yuexun.beilunpatient.config.AppConfig;
import com.yuexun.beilunpatient.ui.registration.adapter.RegistrationDepAdapter;
import com.yuexun.beilunpatient.ui.registration.bean.AppointmentDepBean;
import com.yuexun.beilunpatient.ui.registration.model.impl.RegistrationModel;
import com.yuexun.beilunpatient.ui.registration.presenter.impl.AppointmentDepCenterPresenter;
import com.yuexun.beilunpatient.ui.registration.ui.Act_Registration_Hospital;
import com.yuexun.beilunpatient.ui.registration.ui.view.IAppointmentDepCenterView;
import com.yuexun.beilunpatient.widget.EmptyLayout;

import org.kymjs.kjframe.ui.KJFragment;
import org.kymjs.kjframe.ui.ViewInject;
import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chenss on 2018/3/20 0020.
 */

public class Frag_Registration_Department extends KJFragment implements IAppointmentDepCenterView {
    @Bind(R.id.content_list)
    ListView pullList;
    @Bind(R.id.empty_layout)
    EmptyLayout emptyLayout;

    RegistrationDepAdapter adapter;
    AppointmentDepCenterPresenter depPresenter;
    ArrayList<Object> depBeans = new ArrayList<>();

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.frag_registration_department, null);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initData() {
        depPresenter = new AppointmentDepCenterPresenter(new RegistrationModel(), this);
        ApiGetDep();

        adapter = new RegistrationDepAdapter(pullList, depBeans, R.layout.item_registrationdep);
        pullList.setAdapter(adapter);

        emptyLayout.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
                ApiGetDep();
            }
        });

        pullList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                AppointmentDepBean item = (AppointmentDepBean) adapterView.getItemAtPosition(i);
                Bundle bundle = new Bundle();
                bundle.putString("depId", item.getDepartment_id());
                bundle.putString("depName", item.getDepartment_name());
                Intent intent = new Intent(getActivity(), Act_Registration_Hospital.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });

    }

    private void ApiGetDep(){
        /**
         * mzsj 门诊时间（A上午P下午F全天）(只有resources_type为Y时有效多个组合，例子：A|P|F
         departmentName 科室模糊查询关键字
         scheduleEnddate 排班结束日期yyyy-mm-dd（resources_type为Y时有效）
         scheduleBegdate 排班开始日期yyyy-mm-dd（resources_type为Y时有效）
         resourcesType 资源类型（默认为空，返回所有科室列表，为Y时返回所有可预约科室列表，为D时返回当天有导诊信息的科室列表）
         hospitalId 医院编号（若为空则返回所有中心科室列表，不为空则返回所有该医院科室列表 resourcesType为D时必填）
         */
        Map<String, String> params = new HashMap<>();
        params.put("mzsj", "A|P|F");
        params.put("departmentName", "");
        params.put("scheduleEnddate", "");
        params.put("scheduleBegdate", "");
        params.put("resourcesType", "Y");
        params.put("hospitalId", "");
        params.put("sessionUuidPatient", PreferenceHelper.readString(getActivity(), AppConfig.USER_INFO, AppConfig.SESSION_ID));
        depPresenter.getAppointentDep(params);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void getAppointentDep(BaseEntity<AppointmentDepBean> baseEntity) {
        if (baseEntity.getStatus().equals("200")) {
            depBeans.addAll(baseEntity.getDatas());
            adapter.refresh(depBeans);

        }else {
            ViewInject.toast(baseEntity.getMessage());
        }

        if (depBeans.size() == 0) {
            emptyLayout.setErrorType(EmptyLayout.NODATA);
        } else {
            emptyLayout.dismiss();
        }
    }
}
